package com.bsoft.gol.hcs.utils;

import javax.servlet.http.HttpServletRequest;

/**
 * 请求处理工具类
 *
 * @author tongtp
 * @version v0.1
 * @create 2018-11-28 11:29
 **/
public class RequestUtils {

    public RequestUtils() {
    }

    public static String getRemoteAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        return ip;
    }

    /**
     * @Description:判断请求参数列表中对应名称的参数是否为空_传入参数名称数组的，一个为空则返回为空
     * @param: [request, arrs]
     * @return: boolean
     * @author:tongtp
     * @Date: 2018/11/28
     */
    public static boolean isEmptyInParameter(HttpServletRequest request, String... arrs) {
        int len = arrs.length;

        for (int i = 0; i < len; ++i) {
            if (isEmptyInParameter(request, arrs[i])) {
                return true;
            }
        }

        return false;
    }

    /**
     * @Description:判断请求头中对应名称的参数是否为空_传入参数名称数组的，一个为空则返回为空
     * @param: [request, arrs]
     * @return: boolean
     * @author:tongtp
     * @Date: 2018/11/28
     */
    public static boolean isEmptyInHeader(HttpServletRequest request, String... arrs) {
        int len = arrs.length;

        for (int i = 0; i < len; ++i) {
            if (isEmptyInHeader(request, arrs[i])) {
                return true;
            }
        }

        return false;
    }

    /**
     * @Description:判断请求参数列表中对应名称的参数是否为空
     * @param: [request, name]
     * @return: boolean
     * @author:tongtp
     * @Date: 2018/11/28
     */
    public static boolean isEmptyInParameter(HttpServletRequest request, String name) {
        return CommonUtils.isNull(request.getParameter(name));
    }

    /**
     * @Description:判断请求头中对应名称的参数是否为空
     * @param: [request, name]
     * @return: boolean
     * @author:tongtp
     * @Date: 2018/11/28
     */
    public static boolean isEmptyInHeader(HttpServletRequest request, String name) {
        return CommonUtils.isNull(request.getHeader(name));
    }

    /**
     * @Description:从请求参数列表中获取对应名称的值
     * @param: [request, name]
     * @return: java.lang.String
     * @author:tongtp
     * @Date: 2018/11/28
     */
    public static String getValueFromParameter(HttpServletRequest request, String name) {
        return request.getParameter(name);
    }

    /**
     * @Description:从请求头中获取对应名称的值
     * @param: [request, name]
     * @return: java.lang.String
     * @author:tongtp
     * @Date: 2018/11/28
     */
    public static String getValueFromHeader(HttpServletRequest request, String name) {
        return request.getHeader(name);
    }
}
